package com.fzrskj.cloud.free.user.utils.member;

import com.fzrskj.cloud.free.dic.bean.DicParamItem;
import com.fzrskj.cloud.free.dic.utils.DicParamItemUtils;
import com.fzrskj.cloud.free.user.bean.UserAccount;
import com.fzrskj.cloud.free.user.bean.UserAccountDetail;
import com.fzrskj.cloud.free.user.service.member.vo.DetailImportItemVo;
import com.fzrskj.cloud.free.user.utils.UserAccountUtils;
import com.fzrskj.cloud.free.user.dao.UserAccountDetailDao;
import com.winiis.component.common.utils.bean.ClassUtils;
import ms.core.tool.StrTool;
import org.apache.commons.collections.map.HashedMap;

import java.util.List;
import java.util.Map;

/**
 * MemberImportUtils.
 *
 * @author zj.
 *         Created on 2019/5/8 0008.
 */
public class DetailImportUtils extends Object {
    /**
     * 预先过滤.
     * @param detailList member list.
     * @return member list.
     */
    public List<DetailImportItemVo> refer(List<DetailImportItemVo> detailList) {
        UserAccountUtils accountUtils = new UserAccountUtils();
        DicParamItemUtils dicParamItemUtils = new DicParamItemUtils();
        //遍历
        detailList.forEach(itemVo-> {
            //会员是否存在.
            UserAccount account = accountUtils.queryAccountByName(itemVo.getUserName());
            if (null == account) {
                itemVo.setRemark("会员不存在");
                return;
            }
            if (null == itemVo.getAmount()) {
                itemVo.setRemark("发生额不能为空");
                return;
            }
            if (null == itemVo.getBalance()) {
                itemVo.setRemark("账号余额不能为空");
                return;
            }
            //付款方式检查
            Map<String, Object> payCdt = new HashedMap();
            payCdt.put("paramId", "payType");
            payCdt.put("description", itemVo.getBankCodeName());
            DicParamItem payItem = dicParamItemUtils.querySysParamitemByRule(payCdt);
            if (null == payItem) {
                itemVo.setRemark("不支持付款方式");
                return;
            }
            if (StrTool.isBlankStr(itemVo.getBillNo())) {
                itemVo.setRemark("订单编号不能为空");
                return;
            }
            //记账摘要
            Map<String, Object> itemIdCdt = new HashedMap();
            itemIdCdt.put("paramId", "financeItem");
            itemIdCdt.put("description", itemVo.getItemIdName());
            DicParamItem itemId = dicParamItemUtils.querySysParamitemByRule(itemIdCdt);
            if (null == itemId) {
                itemVo.setRemark("记账摘要不能为空");
                return;
            }
            if (null == itemVo.getRegDate()) {
                itemVo.setRemark("操作时间不能为空");
                return;
            }

            itemVo.setRefer(1);
            itemVo.setRemark("待导入");
        });
        return detailList;
    }

    /**
     * 导入会员.
     * @param memberList member list.
     * @return member list.
     */
    public List<DetailImportItemVo> save(List<DetailImportItemVo> memberList) {
        UserAccountUtils accountUtils = new UserAccountUtils();
        DicParamItemUtils dicParamItemUtils = new DicParamItemUtils();
        UserAccountDetailDao detailDao = new UserAccountDetailDao();

        //遍历
        memberList.forEach(itemVo-> {
            itemVo.setRefer(0);

            //会员是否存在.
            UserAccount account = accountUtils.queryAccountByName(itemVo.getUserName());
            if (null == account) {
                itemVo.setRemark("会员不存在");
                return;
            }
            if (null == itemVo.getAmount()) {
                itemVo.setRemark("发生额不能为空");
                return;
            }
            if (null == itemVo.getBalance()) {
                itemVo.setRemark("账号余额不能为空");
                return;
            }
            //付款方式检查
            Map<String, Object> payCdt = new HashedMap();
            payCdt.put("paramId", "payType");
            payCdt.put("description", itemVo.getBankCodeName());
            DicParamItem payItem = dicParamItemUtils.querySysParamitemByRule(payCdt);
            if (null == payItem) {
                itemVo.setRemark("不支持付款方式");
                return;
            }
            if (StrTool.isBlankStr(itemVo.getBillNo())) {
                itemVo.setRemark("订单编号不能为空");
                return;
            }
            //记账摘要
            Map<String, Object> itemIdCdt = new HashedMap();
            itemIdCdt.put("paramId", "financeItem");
            itemIdCdt.put("description", itemVo.getItemIdName());
            DicParamItem itemId = dicParamItemUtils.querySysParamitemByRule(itemIdCdt);
            if (null == itemId) {
                itemVo.setRemark("记账摘要不能为空");
                return;
            }
            if (null == itemVo.getRegDate()) {
                itemVo.setRemark("操作时间不能为空");
                return;
            }

            UserAccountDetail accountDetail = new UserAccountDetail();
            accountDetail.setAmount(itemVo.getAmount());
            accountDetail.setBalance(itemVo.getBalance());
            accountDetail.setBankCode(payItem.getValue());
            accountDetail.setBillno(itemVo.getBillNo());
            accountDetail.setItemId(Integer.valueOf(itemId.getValue()));
            accountDetail.setOperator(account.getUserName());
            accountDetail.setRegDate(itemVo.getRegDate());
            accountDetail.setRemark(itemVo.getRemarkRecord());
            accountDetail.setSnId(itemVo.getSnId());
            accountDetail.setVoucherId(itemVo.getVoucherId());
            accountDetail.setStatus(1);
            accountDetail.setUserId(account.getUserId());
            accountDetail.setUserName(account.getUserName());
            Integer affect = detailDao.addUserAccountdetail(ClassUtils.beanToMap(accountDetail));
            if (affect < 1) {
                itemVo.setRemark("添加记录失败");
                return;
            }

            itemVo.setRemark("导入成功");
        });
        return memberList;
    }
}
